﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Archives.Common.Utils;
using Archives.Models;

namespace Archives.Common.Attributes
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class RolesAttribute : AuthorizeAttribute 
    {
        public  Enums.ROLES[] roles{ get; set; }
        public int Id { get; set; }

         public RolesAttribute(params Enums.ROLES[] Roles)
        {
            this.roles = Roles;
        }

         protected override bool AuthorizeCore(HttpContextBase httpContext)
         {
             bool authorize = false;
             //foreach (var role in roles)
             //{
             //    ArchivesContext context = new ArchivesContext();
             //    var user = context.Users.Where(m => m.ro == role &&
             //    m.IsActive == true); // checking active users with allowed roles.  
             //    if (user.Count() > 0)
             //    {
             //        authorize = true; /* return true if Entity has current user(active) with specific role */
             //    }
             //}
             return authorize;
         }


         protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
         {
             filterContext.Result = new HttpUnauthorizedResult();
         }  
    }
}